package com.software.messagecenter.controller;

import com.software.messagecenter.entity.ClassMessage;
import com.software.messagecenter.entity.ClassMessageDTO;
import com.software.messagecenter.service.ClassMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/class_message")
public class ClassMessageController {

    @Autowired
    private ClassMessageService classMessageService;

    @GetMapping("/findByClassId/{classId}")
    public List<ClassMessageDTO> findByClassId(@PathVariable String classId){
        List<ClassMessageDTO> classMessageDTOList = classMessageService.findByClassId(classId);
        return classMessageDTOList;
    }

    @GetMapping("/findMessagesByClassId/{classId}")
    public List<ClassMessage> findMessagesByClassId(@PathVariable String classId){
        List<ClassMessage> classMessageList = classMessageService.findMessagesByClassId(classId);
        return classMessageList;
    }

    @PostMapping("/addClassMessage")
    public ClassMessage addClassMessage(@RequestBody ClassMessage classMessage){
        System.out.println(classMessage);
        ClassMessage create = new ClassMessage();
        create.setClassId(classMessage.getClassId());
        create.setContent(classMessage.getContent());
        create.setSendId(classMessage.getSendId());
        create.setMessageId(UUID.randomUUID().toString());
        Date date = new Date();
        create.setSendTime(date);
        classMessageService.addClassMessage(create);
        return create;
    }


}
